<%@ page import="dao.StudentsDao" %>
<%@ page import="dao.impl.StudentsDaoimpl" %>
<%@ page import="dao.RegisterDao" %>
<%@ page import="dao.impl.RegisterDaoimpl" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 98vh;
        }

        .login-container {
            background-color: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
            width: 100%;
            max-width: 420px;
            position: relative;
            overflow: hidden;
        }

        .login-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 4px;
            background: linear-gradient(90deg, #4a90e2, #36d1dc);
        }

        .login-header h1 {
            color: #333;
            font-size: 26px;
            margin-bottom: 10px;
            font-weight: 600;
        }

        .form-group input {
            width: 100%;
            padding: 14px;
            border: 1px solid #e0e0e0;
            border-radius: 6px;
            font-size: 16px;
            transition: all 0.3s;
            background-color: #f9f9f9;
        }

        .form-group input:focus {
            border-color: #4a90e2;
            outline: none;
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.2);
            background-color: white;
        }

        .login-button {
            width: 100%;
            padding: 14px;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .login-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
        }

        .remember-forgot {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .remember-forgot1 {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .remember-me {
            display: flex;
            align-items: center;
        }

        .remember-me input {
            margin-right: 8px;
        }

        .forgot-password a {
            color: #4a90e2;
            text-decoration: none;
        }


        .login-button {

            width: 100%;
            padding: 12px;
            background-color: #0050ad;
            color: #ffffff;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .login-button:hover {
            background-color: #3a7bc8;
        }

        .register-link {
            text-align: center;
            margin-top: 20px;
            color: #666;
        }

        .register-link a {
            color: #4a90e2;
            text-decoration: none;
        }
        .error{
            color: #ff4444;
            font-size: 14px;
            margin-top: 5px;
            padding: 5px 10px;
            background-color: #ffeeee;
            border-radius: 4px;
            border-left: 3px solid #ff4444;
            display: none; /* 初始隐藏 */
            animation: fadeIn 0.3s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-5px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .form-group {
            position: relative;
            margin-bottom: 24px;
        }

        .form-group label {
            position: absolute;
            left: 14px;
            top: 14px;
            color: #888;
            font-weight: 400;
            transition: all 0.3s;
            pointer-events: none;
            padding: 0 5px;
        }

        .form-group input:focus + label,
        .form-group input:not(:placeholder-shown) + label {
            top: -10px;
            left: 10px;
            font-size: 12px;
            color: #4a90e2;
            background: white;
        }

        .loader {
            display: inline-block;
            width: 16px;
            height: 16px;
            border: 2px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 1s ease-in-out infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .form-group input::placeholder {
            color: transparent;
        }

        .form-group input:not(:placeholder-shown) {
            padding-top: 20px;
            padding-bottom: 8px;
        }

        .password-toggle {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #888;
            font-size: 14px;
        }
        .zc{
            position: relative;
            top: -20px;
        }

    </style>

</head>
<body>

<%--<%--%>
<%--    request.setCharacterEncoding("utf-8");--%>
<%--    String a=request.getParameter("username");--%>
<%--    String b=request.getParameter("full_name");--%>
<%--    String c=request.getParameter("password");--%>
<%--    String d=request.getParameter("option");--%>
<%--    if (a != null) {--%>
<%--        RegisterDao dao=new RegisterDaoimpl();--%>
<%--        dao.Register(a,b,c,d);--%>

<%--    }--%>
<%--%>--%>

<img src="../images/enroll/【哲风壁纸】原野-月-月色.png" class="peijing" style="position: absolute;z-index: -1;width: 2000px;height: 1000px">
<div class="login-container">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
        <h1 class="zc">用户注册</h1>

    </div>
    <form id="registerForm" action="<%=request.getContextPath()%>/RegisterController" method="post">
        <input type="hidden" name="op" value="Register">
        
        <div class="form-group">
            <input class="ys" type="text" id="username" name="username" placeholder=" " required>
            <label for="username">用户名</label>
            <div id="usernameError" class="error"></div>
        </div>

        <div class="form-group">
            <input type="tel" id="phone" name="full_name" placeholder=" " required>
            <label for="phone">手机号码</label>
            <div id="phoneError" class="error"></div>
        </div>
        <div class="form-group">
            <input type="password" id="password" name="password" placeholder=" " required>
            <label for="password">密码</label>
            <div id="passwordError" class="error"></div>
        </div>

        <div class="form-group">
            <input type="password" id="confirmPassword" name="password" placeholder=" " required>
            <label for="confirmPassword">确认密码</label>
            <div id="confirmPasswordError" class="error"></div>
        </div>

        <div class="remember-forgot1">
            <label>性别:</label>
            <label>
                <input type="radio" name="sex" value="男">
                <span class="radio-custom"></span>
                男
            </label>
            <label>
                <input type="radio" name="sex" value="女">
                <span class="radio-custom"></span>
                女
            </label>
        </div>
        <div class="remember-forgot">
            <div class="remember-me">
                <input type="checkbox" id="remember" name="remember" >
                <label for="remember">记住我</label>
            </div>
            <label>
                <input type="radio" name="userType" value="student">
                <span class="radio-custom"></span>
                学生
            </label>
            <label>
                <input type="radio" name="userType" value="dormitory">
                <span class="radio-custom"></span>
                宿管
            </label>


            <%--            <div class="forgot-password">--%>
            <%--                <a href="/forgot-password">忘记密码?</a>--%>
            <%--            </div>--%>

        </div>



        <div class="form-group">
            <button type="button" id="openCameraBtn" class="login-button" style="background-color: #6c757d;">
                打开摄像头
            </button>
            <input type="hidden" id="facePhoto" name="facePhoto">
        </div>


        <button type="submit" class="login-button">注册</button>
    </form>





    <div id="cameraModal" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.7);z-index:1000;justify-content:center;align-items:center;">
        <div style="background:white;padding:20px;border-radius:8px;width:80%;max-width:500px;">
            <div style="text-align:right;">
                <button id="closeCameraBtn" style="background:none;border:none;font-size:20px;">×</button>
            </div>
            <video id="video" width="100%" autoplay style="display:none;"></video>
            <canvas id="canvas" width="400" height="300" style="display:none;"></canvas>
            <div id="photoPreview" style="text-align:center;"></div>
            <button id="captureBtn" class="login-button" style="margin-top:15px;width:100%;">拍摄照片</button>
        </div>
    </div>

</div>
<script>
    document.getElementById('registerForm').addEventListener('submit', function(e) {
        e.preventDefault();

        // 清除之前的错误信息
        const errorElements = document.querySelectorAll('.error');
        errorElements.forEach(el => {
            el.textContent = '';
            el.style.display = 'none';
        });

        // 获取输入值
        const username = document.getElementById('username').value.trim();
        const phone = document.getElementById('phone').value.trim();
        const password = document.getElementById('password').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        const facePhoto = document.getElementById('facePhoto').value;


        let isValid = true;

        const usernameRegex = /^[\u4e00-\u9fa5a-zA-Z0-9_]{2,16}$/;
        if (!usernameRegex.test(username)) {
            const errorEl = document.getElementById('usernameError');
            errorEl.textContent = '用户名应为4-16位中文、字母、数字或下划线';
            errorEl.style.display = 'block';
            isValid = false;
        }

        // 验证手机号
        const phoneRegex = /^1[3-9]\d{9}$/;
        if (!phoneRegex.test(phone)) {
            const errorEl = document.getElementById('phoneError');
            errorEl.textContent = '请输入有效的11位手机号码';
            errorEl.style.display = 'block';
            isValid = false;
        }

        // 验证密码
        const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/;
        if (!passwordRegex.test(password)) {
            const errorEl = document.getElementById('passwordError');
            errorEl.textContent = '密码应为8-20位字符，包含字母和数字';
            errorEl.style.display = 'block';
            isValid = false;
        }

        // 验证确认密码
        if (password !== confirmPassword) {
            const errorEl = document.getElementById('confirmPasswordError');
            errorEl.textContent = '两次输入的密码不一致';
            errorEl.style.display = 'block';
            isValid = false;
        }

        // 添加人脸数据验证
        if (!facePhoto) {
            alert('请先拍摄人脸照片！');
            e.preventDefault();
            return;
        }

        if (isValid) {
            const button = document.querySelector('.login-button');
            button.innerHTML = '<span class="loader"></span> 正在注册...';
            button.disabled = true;

            setTimeout(() => {
                alert('注册成功！');
                this.submit();
            }, 1500);
        }
    });
    function togglePassword(id) {
        const input = document.getElementById(id);
        const toggle = input.nextElementSibling.nextElementSibling;
        if (input.type === 'password') {
            input.type = 'text';
            toggle.textContent = '隐藏';
        } else {
            input.type = 'password';
            toggle.textContent = '显示';
        }
    }


    // 摄像头相关变量
    let stream = null;


    // 打开摄像头
    document.getElementById('openCameraBtn').addEventListener('click', function() {
        const modal = document.getElementById('cameraModal');
        const video = document.getElementById('video');
        const canvas = document.getElementById('canvas');

        modal.style.display = 'flex';
        video.style.display = 'block';
        canvas.style.display = 'none';

        navigator.mediaDevices.getUserMedia({ video: true })
            .then(function(s) {
                stream = s;
                video.srcObject = stream;
            })
            .catch(function(err) {
                console.error('摄像头访问错误:', err);
                alert('无法访问摄像头: ' + err.message);
                modal.style.display = 'none';
            });
    });
    // 关闭摄像头
    document.getElementById('closeCameraBtn').addEventListener('click', function() {
        closeCamera();
    });

    // 拍摄照片
    document.getElementById('captureBtn').addEventListener('click', function() {
        const video = document.getElementById('video');
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        const photoPreview = document.getElementById('photoPreview');

        // 绘制当前视频帧到canvas
        ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

        // 显示拍摄的照片
        photoPreview.innerHTML = '';
        const img = document.createElement('img');
        img.src = canvas.toDataURL('image/png');
        img.style.maxWidth = '100%';
        photoPreview.appendChild(img);

        // 保存照片数据到隐藏字段
        document.getElementById('facePhoto').value = canvas.toDataURL('image/png');

        // 关闭摄像头和模态框
        closeCamera();
    });


    // 关闭摄像头函数
    function closeCamera() {
        const modal = document.getElementById('cameraModal');
        const video = document.getElementById('video');

        if (stream) {
            stream.getTracks().forEach(track => track.stop());
            stream = null;
        }

        modal.style.display = 'none';
        video.style.display = 'none';
    }
</script>
</body>
</html>


